
//
//  SubjectDetailsViewController.m
//  xiaoyiNew
//
//  Created by TW on 2017/2/8.
//  Copyright © 2017年 讯一. All rights reserved.
//

#import "SubjectDetailsViewController.h"
#import "SubjectDetailsTableViewCell.h"
#import "SubjectMessageView.h"
#import "SubjectDetailsBottonView.h"
#import "BeansViewController.h"
#import "SubjectDetailsHeader.h"

@interface SubjectDetailsViewController ()<UITableViewDelegate, UITableViewDataSource, SubjectDetailsBottonViewDelegate>{

    BOOL _ifMessage;//是否显示消息
}
@property(nonatomic, strong) UITableView * tableView;//tableView
@property(nonatomic, strong) SubjectDetailsBottonView * detailsBotton;//底下bottom
@property(nonatomic, strong) SubjectMessageView * messageView;//发送界面
@property(nonatomic, strong) SubjectDetailsHeader * headerView;//头视图


@end

@implementation SubjectDetailsViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    [self initializeDataSource];
    [self initializeUserInterface];
    [self initializeConstraints];
    // Do any additional setup after loading the view.
}


- (void)initializeDataSource {
    //更新数据
    _ifMessage = NO;
    
}
- (void)initializeUserInterface {
    //初始化界面
    self.title = @"主题详情";
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"back"] style:UIBarButtonItemStylePlain target:self action:@selector(action_balckClick)];
    
    [self.view addSubview:self.tableView];
    [self.view addSubview:self.detailsBotton];
    [self.view addSubview:self.messageView];
    
    
}
- (void)initializeConstraints {
    //设置约束
    [self.detailsBotton mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.bottom.equalTo(self.view);
        make.size.mas_equalTo(CGSizeMake(Screen_Width, 45));
    }];
    [self.messageView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.right.equalTo(self.view);
        make.height.mas_equalTo(45);
        make.bottom.equalTo(self.detailsBotton.mas_top);
    }];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).offset(64);
        make.left.right.equalTo(self.view);
        make.bottom.equalTo(self.detailsBotton.mas_top);
    }];
    
}

#pragma mark -  点击 -
//返回
- (void)action_balckClick {
    
    [self.navigationController popViewControllerAnimated:YES];
}
#pragma mark - SubjectDetailsBottonViewDelegate -
//功能
- (void)bottonClickWithIndex:(NSInteger)index {

    switch (index) {
        case 0:{
            //点赞
            NSLog(@"点赞");
        }
            break;
        case 1:{
            //消息
            NSLog(@"消息");
            _ifMessage = !_ifMessage;
            if (_ifMessage) {
                //显示
                self.messageView.hidden = NO;
//                [self.tableView mas_updateConstraints:^(MASConstraintMaker *make) {
//                    make.bottom.equalTo(self.messageView.mas_top);
//                }];
            }else {
            
                self.messageView.hidden = YES;
//                [self.tableView mas_updateConstraints:^(MASConstraintMaker *make) {
//                    make.bottom.equalTo(self.detailsBotton.mas_top);
//                }];
            }
        }
            break;
        case 2:{
            //分享
            NSLog(@"分享");
        }
            break;
        case 3:{
            //撒豆
            NSLog(@"撒豆");
            BeansViewController * beansVC = [[BeansViewController alloc] init];
            [self.navigationController pushViewController:beansVC animated:YES];
        }
            break;
        default:
            break;
    }

}
#pragma mark - UITableViewDelegate, UITableViewDataSource -

//行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return 2;
}
//高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return 100;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {

    return self.headerView;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {

    NSArray * images = [self.model.imageurl componentsSeparatedByString:@"|"];
    NSMutableArray * mArr = [NSMutableArray array];
    for (NSString * str in images) {
        if (str.length > 0) {
            [mArr addObject:str];
        }
    }
    NSInteger page;
    if (mArr.count == 0) {
        page = 0;
    }else if (mArr.count > 0 && mArr.count <=3){
        page = 1;
    }else if (mArr.count > 3 && mArr.count <=6){
        
        page = 2;
    }else {
        
        page = 3;
    }
    return 160 + page*115;
}
//cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    SubjectDetailsTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    return cell;
}
//cell点击
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    NSLog(@"%ld",indexPath.row);
}

#pragma mark - lazy -
- (SubjectDetailsHeader *)headerView {

    
    if (!_headerView) {
        _headerView = [[[NSBundle mainBundle] loadNibNamed:@"SubjectDetailsHeader" owner:nil options:nil] firstObject];
        _headerView.model = self.model;
    }
    return _headerView;
    
}
- (UITableView *)tableView {
    
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, Screen_Width, Screen_Height) style:UITableViewStyleGrouped];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        [_tableView registerNib:[UINib nibWithNibName:@"SubjectDetailsTableViewCell" bundle:nil] forCellReuseIdentifier:@"cell"];
        [_tableView setBackgroundColor:[UIColor yellowColor]];
    }
    return _tableView;
    
}

- (SubjectDetailsBottonView *)detailsBotton {

    
    if (!_detailsBotton) {
        _detailsBotton = [[[NSBundle mainBundle] loadNibNamed:@"SubjectDetailsBottonView" owner:nil options:nil] firstObject];
        _detailsBotton.delegate = self;
        
    }
    return _detailsBotton;
    
}
- (SubjectMessageView *)messageView {

    
    if (!_messageView) {
        _messageView = [[SubjectMessageView alloc] init];
        _messageView.hidden = YES;
    }
    return _messageView;
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
